package   ybt.demo.web.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import   ybt.demo.custom.AjaxResult;
import   ybt.demo.custom.TablePageable;
import   ybt.demo.entity.Course;
import   ybt.demo.entity.Grade;
import   ybt.demo.entity.Student;
import   ybt.demo.entity.SysUser;
import   ybt.demo.entity.TbClass;
import   ybt.demo.security.UserUtils;
import   ybt.demo.service.CourseService;
import   ybt.demo.service.GradeService;
import   ybt.demo.service.StudentService;
import   ybt.demo.service.TbClassService;
import   ybt.demo.web.form.GradeForm;

@Controller
@RequestMapping(value = "/grade")
public class GradeController {
	@Autowired
	private GradeService gradeService;
	@Autowired
	private UserUtils userUtils;
	@Autowired
	private TbClassService tbclassService;
	@Autowired
	private CourseService courseService;
	@Autowired
	private StudentService studentService;

	@RequestMapping(value = "/peasonGrade")
	public String peasonGrade(ModelMap modelMap) {
		SysUser user = userUtils.getUser();
		Integer id = user.getId();
		List<Grade> list = gradeService.findByStudentId(id);
		modelMap.put("list", list);
		return "grade/peasonGrade";
	}

//  班级成绩录入
	@RequestMapping(value = "/bjcjlr")
	public String bjcjlr(ModelMap modelMap) {
		List<TbClass> tbClasses = tbclassService.findAll();
		List<Course> courses = courseService.findAll();
		modelMap.put("bjs", tbClasses);
		modelMap.put("kcs", courses);
		return "grade/bjcjlr";
	}

//显示表
	@RequestMapping(value = "/bx")
	@ResponseBody
	public String bjcj(GradeForm form, Integer bjId, Integer kcId, ModelMap modelMap) {
		List<Student> students = studentService.findByTbClassId(bjId);
		List<Grade> grades = new ArrayList<Grade>();
		for (Student s : students) {
			Grade grade = gradeService.findByStudentIdAndCourseId(s.getId(), kcId);
			if (grade == null) {
				Grade grade1 = new Grade();
				grade1.setCourse(courseService.findById(kcId));
				grade1.setStudent(studentService.findById(s.getId()));
				gradeService.save(grade1);
				grades.add(grade1);
			} else {
				grades.add(grade);
			}
		}
		modelMap.put("grades", grades);
		return "grade/bx";

	}

//修改成绩
	@RequestMapping(value = "/cjChange")
	@ResponseBody
	public void cjChange(Integer id, Integer grade) {
		Grade g = gradeService.findById(id);
		g.setGrade(grade);
		gradeService.save(g);

	}

	// 个人成绩录入
	@RequestMapping(value = "/grcjgl")
	public String personGradelr(ModelMap modelMap) {
		List<TbClass> tbClasses = tbclassService.findAll();

		modelMap.put("list", tbClasses);
		return "grade/grcjgl";
	}

	// 从班级获取学生名单
	@RequestMapping(value = "/getStu")
	@ResponseBody
	public Object getStu(Integer bjId, ModelMap modelMap) {
		List<Student> students = studentService.findByTbClassId(bjId);
		return students;
	}

	// 得到学生成绩
	@RequestMapping(value = "/getPersonGrade")
	@ResponseBody
	public Object getPersonGrade(Integer stuId, ModelMap modelMap) {
		List<Grade> grades = gradeService.findByStudentId(stuId);
		modelMap.put("list", grades);
		return grades;
	}

	// 修改成绩
	@RequestMapping(value = "/cjChange1")
	@ResponseBody
	public void cjChange1(Integer id, Integer grade) {
		Grade grade2 = gradeService.findById(id);
		grade2.setGrade(grade);
		gradeService.save(grade2);
	}

	// 班级成绩查询
	@RequestMapping(value = "/bjcjcx")
	public String bjcjcx(ModelMap modelMap) {
		List<TbClass> list = tbclassService.findAll();
		modelMap.put("list", list);
		return "grade/bjcjcx";
	}

	@SuppressWarnings("serial")
	public Specification<Grade> buildSpec(Integer bjId, Integer kcId) {
		Specification<Grade> spec = new Specification<Grade>() {

			@Override
			public Predicate toPredicate(Root<Grade> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
				if (bjId != null) {
					rules.add(cb.equal(root.get("student").get("tbClass").get("id").as(String.class), bjId));
				}
				if (kcId != null) {
					rules.add(cb.equal(root.get("course").get("id").as(String.class), kcId));
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}

	@RequestMapping(value = "/page")
	@ResponseBody
	public Object page(TablePageable tablePaegable, Integer bjId, Integer kcId) {
		List<Student> students = studentService.findByTbClassId(bjId);
		for (Student s : students) {
			Grade grade = gradeService.findByStudentIdAndCourseId(s.getId(), kcId);
			if (grade == null) {
				Grade grade1 = new Grade();
				grade1.setCourse(courseService.findById(kcId));
				grade1.setStudent(studentService.findById(s.getId()));
				grade1.setGrade(0);
				gradeService.save(grade1);
			}
		}
		PageRequest pageable = tablePaegable.buildPageRequest();
		Specification<Grade> spec = buildSpec(bjId, kcId);
		Page<Grade> page = gradeService.findAll(spec, pageable);
		return AjaxResult.buildTableResult(page);
	}

	@RequestMapping(value = "/page1")
	@ResponseBody
	public Object page1(TablePageable tablePaegable, Integer bjId, Integer stuId) {
		List<Student> students = studentService.findByTbClassId(bjId);
		List<Grade> grades = gradeService.findByStudentId(stuId);
		PageRequest pageable = tablePaegable.buildPageRequest();
		Specification<Grade> spec = buildSpec(bjId, stuId);
		Page<Grade> page = gradeService.findAll(spec, pageable);
		return AjaxResult.buildTableResult(page);
	}
	@RequestMapping(value = "/classlrGrade")
	public String bjcjlr1(ModelMap modelMap) {
		List<TbClass> tbClasses = tbclassService.findAll();
		List<Course> courses = courseService.findAll();
		modelMap.put("bjs", tbClasses);
		modelMap.put("kcs", courses);
		return "grade/classlrGrade";
	}

}
